<?php
# images path
$IMAGE_PATH = 'images/store/';

# URL variable
$arg = "id";

# include pic and info context if ID is set
if(isset($_GET[$arg])) {
	# Image id
	$id = $_GET[$arg];
	
	$conn = connectDB();
	
	// query DB
	$result = queryDB("SELECT id, filename, date_time, category, gps_latitude, gps_longitude, location_name, country, temperature FROM image WHERE ID=$id", $conn);
	$tag_result = queryDB("SELECT tag FROM tags WHERE picid=$id", $conn);
	
	$tag_row = $row = mysql_fetch_array($tag_result , MYSQL_NUM);
	$tag_num = mysql_numrows($tag_result);
	
	$num = mysql_numrows($result);

	
	// get row of results from query
	$row = mysql_fetch_array($result, MYSQL_NUM);
	
	closeDB($conn);
	
	$img = $IMAGE_PATH.$row[1];
		
	echo "
			
		<table width=\"610\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
			<tr>

				<td colspan=\"2\">";
	echo '<center><img src="' .$img. '" ' .width_scale($img,"610"). '></center>';
				
	echo "</td>
			<tr>
				<td width=\"180\" height=\"20\"></td>
				<td width=\"585\" height=\"20\"></td>
			</tr>
			<tr>
				<td height=\"20\">ID:</td>
				<td width=\"585\" height=\"20\">$row[0]</td>
			</tr>
			<tr>
				<td height=\"20\">Category:</td>
				<td width=\"585\" height=\"20\">$row[3]</td>
			</tr>
			<tr>
				<td height=\"20\">Filename:</td>
				<td height=\"20\">$row[1]</td>
			</tr>
			<tr>
				<td height=\"20\">Date/time:</td>
				<td height=\"20\">$row[2]</td>
			</tr>
			<tr>
				<td height=\"20\">Location name:</td>
				";
				if($row[7] == NULL) {
					echo "<td height=\"20\">N/A</td>";
				} else {
					echo "<td height=\"20\">".$row[6]."</td>";
				}
			echo "
			</tr>
			<tr>
				<td height=\"20\">Country:</td>";
				if($row[7] == NULL) {
					echo "<td height=\"20\">N/A</td>";
				} else {
					echo "<td height=\"20\">".utf8_encode($row[7])."</td>";
				}
			echo "
			</tr>
			<tr>
				<td height=\"20\">GPS coordinates:</td>";
				if($row[7] == NULL) {
					echo "<td height=\"20\">N/A</td>";
				} else {
					echo "<td height=\"20\">Lat: ".$row[4]." Lon: ".$row[5]."</td>";
				}
			echo "
			</tr>
			<tr>
				<td height=\"20\">Weather data:</td>";
				if($row[8] == NULL) {
					echo "<td height=\"20\">N/A</td>";
				} else {
					echo "<td height=\"20\">Approximate temperature: ".$row[8]." C</td>";
				}
			echo "
			<tr>
				<td width=\"220\" height=\"20\">Panoramio top 5 tags:</td>
				<td height=\"20\">";
				$c = 1;
				while ($c < $tag_num) {
					$tag_row = mysql_fetch_array($tag_result, MYSQL_NUM);
					echo "'".utf8_encode($tag_row[0])."' ";
					$c = $c + 1;
				}				
				echo" </td>
			</tr>
			<tr>
				<td colspan =\"2\" height=\"20\"><center><h3>Placement on map</h3></center></td>
			</tr>
			<tr>";
			if ($row[7] == NULL) {
				echo "<td colspan=\"2\"><center>N/A</center></td>";
			} else {
			echo "<td colspan=\"2\"><div id=\"map\" style=\"width: 610px; height: 300px\"></div></td>";
			}
			echo "
			</tr>
			<tr>
				<td colspan=\"2\"><center><h3>Geographically related images - Panoramio&copy;</h3></center></td>
			</tr>
			<tr>";
			if ($row[7] == NULL) {
				echo "<td colspan=\"2\"><center>N/A</center></td>";
			} else {
				echo "<td colspan=\"2\"><iframe style=\"border: none;\" src=\"http://www.panoramio.com/plugin.php?lt=$row[4]&ln=$row[5]&z=5&k=0\" width=\"610px\" height=\"300px\"></iframe></td>";
			}
			echo "
			</tr>
		</table>";

}
?>

<script type="text/javascript">
//<![CDATA[
<?php
	$arg_img = "id";
	$id = $_GET[$arg_img];
	
	$conn = connectDB();
	
	// query DB
	$result = queryDB("SELECT gps_latitude, gps_longitude FROM image WHERE ID=$id", $conn);
	
	$num = mysql_numrows($result);
	
	// get row of results from query
	$row = mysql_fetch_array($result, MYSQL_NUM);

	echo 'var map = new GMap2(document.getElementById("map"));';
	echo 'map.addControl(new GLargeMapControl());';
	echo 'map.addControl(new GMapTypeControl());';
	echo 'map.addControl(new GScaleControl());';
	echo 'map.setCenter(new GLatLng('.$row[0].','. $row[1].'), 12, G_NORMAL_MAP);';

	// Creates a marker whose info window displays the given number
	echo '	function createMarker(point, number)';
	echo '	{';
	echo '		var marker = new GMarker(point);';
	// Show this markers index in the info window when it is clicked
	echo '		var html = number;';
	echo '		GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});';
	echo '		return marker;';
	echo '	};';
	
	closeDB($conn);
	
	echo "var point = new GLatLng(" . $row[0] . "," . $row[1] . ");\n";
#	echo "var marker = createMarker(point, '" . addslashes("HEIATIL") . "');\n";
#	echo "map.addOverlay(marker);\n";
	echo "map.addOverlay(new GMarker(point));";
	echo "\n";
?>

//]]>
</script>
<?php
	echo '<a name="flickr"></a>';
	echo '<br><center><h3>Flickr related images:</h3></center>';
	
	$month = array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
	$arg_img = "id";
	$id = $_GET[$arg_img];
	
	$conn = connectDB();
	
	// query DB
	$result = queryDB("SELECT gps_latitude, gps_longitude FROM image WHERE ID=$id", $conn);
	
	$num = mysql_numrows($result);
	
	// get row of results from query
	$row = mysql_fetch_array($result, MYSQL_NUM);	
	

	# if geolocation
	if($row[0] != NULL) {
		echo '<table width="610" border="0" cellspacing="5" cellpadding="0">';
			
		# check id date is set			
		if(count($_POST) > 0) {
			if ((int)$_POST['fmonth'] < 10) {
				$_POST['fmonth'] = "0".$_POST['fmonth'];
			}
			if ((int)$_POST['fday'] < 10) {
				$_POST['fday'] = "0".$_POST['fday'];
			}
			if ((int)$_POST['tmonth'] < 10) {
				$_POST['tmonth'] = "0".$_POST['tmonth'];
			}
			if ((int)$_POST['tday'] < 10) {
				$_POST['tday'] = "0".$_POST['tday'];
			}
			$from_date = $_POST['fyear']."-".$_POST['fmonth']."-".$_POST['fday']." 00:00:00";
			$to_date = $_POST['tyear']."-".$_POST['tmonth']."-".$_POST['tday']." 23:59:59";
			
			# get the images for posted date
			$flickr_images = getFlickrImages($row[0], $row[1], $from_date, $to_date );
		}
		else {
			# get the images for standard last year date
			$flickr_images = getFlickrImages($row[0], $row[1]);
		}
		
		$i = 0;
		echo '<center>';
		foreach ($flickr_images as $img_html) {
			echo $img_html."&nbsp;";

			$i = $i + 1;
			if ($i == 5) {
				echo '<br>';
				$i = 0;
			}
		}
		# get current url
		$currurl = curPageURL();
		
		echo '</table>';	
		echo "<center><br>Only show images taken between dates:</center><br>";
		echo '
		

    <center>
		<form id="form1" name="date" method="post" action="'.$currurl.'#flickr">
      <label>Day
      <select name="fday" id="fday">';
				
				for($i = 1; $i <= 31;  $i += 1) {
					if(isset($_POST['fday'])) {
						if($i == $_POST['fday']) {
							echo '<option value="'.$i.'" selected="selected" >'.$i.'</option>';
						}
						else {
							echo '<option value="'.$i.'">'.$i.'</option>';
						}
					}
					else {
						echo '<option value="'.$i.'">'.$i.'</option>';
					}
				}
			echo '
      </select>
      </label>
      <label>Month
      <select name="fmonth" id="fmonth">';
			
				for($i = 1; $i <= 12;  $i += 1) {
					if(isset($_POST['fmonth'])) {
						if($i == $_POST['fmonth']) {
							echo '<option value="'.$i.'"  selected="selected" >'.$month[$i].'</option>';
						}
						else {
							echo '<option value="'.$i.'">'.$month[$i].'</option>';
						}
					}
					else {
						echo '<option value="'.$i.'">'.$month[$i].'</option>';
					}
				}
			echo '
      </select>
      </label>
      <label>Year
      <select name="fyear" id="fyear">';
				for($i = 1997; $i <= 2009;  $i += 1) {
					if(isset($_POST['fyear'])) {
						if($i == $_POST['fyear']) {
							echo '<option value="'.$i.'"  selected="selected" >'.$i.'</option>';
						}
						else {
							echo '<option value="'.$i.'">'.$i.'</option>';
						}
					}
					else {
						echo '<option value="'.$i.'">'.$i.'</option>';
					}
				}
			echo '
      </select>
      </label>
			<br>
      <label>Day
      <select name="tday" id="tday">';
				
				for($i = 1; $i <= 31;  $i += 1) {
					if(isset($_POST['tday'])) {
						if($i == $_POST['tday']) {
							echo '<option value="'.$i.'" selected="selected" >'.$i.'</option>';
						}
						else {
							echo '<option value="'.$i.'">'.$i.'</option>';
						}
					}
					else {
						echo '<option value="'.$i.'">'.$i.'</option>';
					}
				};
			echo '
      </select>
      </label>
      <label>Month
      <select name="tmonth" id="tmonth">';
			
				for($i = 1; $i <= 12;  $i += 1) {
					if(isset($_POST['tmonth'])) {
						if($i == $_POST['tmonth']) {
							echo '<option value="'.$i.'"  selected="selected" >'.$month[$i].'</option>';
						}
						else {
							echo '<option value="'.$i.'">'.$month[$i].'</option>';
						}
					}
					else {
						echo '<option value="'.$i.'">'.$month[$i].'</option>';
					}
				}
			echo '
      </select>
      </label>
      <label>Year
      <select name="tyear" id="tyear">';
				for($i = 1997; $i <= 2009;  $i += 1) {
					if(isset($_POST['tyear'])) {
						if($i == $_POST['tyear']) {
							echo '<option value="'.$i.'"  selected="selected" >'.$i.'</option>';
						}
						else {
							echo '<option value="'.$i.'">'.$i.'</option>';
						}
					}
					else {
						echo '<option value="'.$i.'">'.$i.'</option>';
					}
				}
			echo '
      </select>
      </label>
		  <br />
			<label>
  		<input type="submit" name="update" id="update" value="Update" />
  		</label>
    </form>
		</center>';
		echo '</center>';

	}
	
	else {
		echo '<center>N/A</center>';
		echo '<br>';	
	}
	
?>
<?php 
	# insert wikipedia related material
	echo '<br><center><h3>Geographically related article(s) - Wikipedia</h3></center>';
	# URL variable
	$arg = "id";
	
	if ($row[0] == NULL) {
		echo "<center>N/A</center>";
	}
	else {
		# include pic and info context if ID is set
		if(isset($_GET[$arg])) {
			# Image id
			$id = $_GET[$arg];
			
			$conn = connectDB();
			
			// query DB
			$result = queryDB("SELECT title, summary, wikiurl, distance FROM wikiarticle WHERE picid=$id", $conn);
			
			$num = mysql_numrows($result);
			
			closeDB($conn);
			
			if($num) {
				echo '<table width="610" border="0" cellspacing="5" cellpadding="0">';
					
					while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
						echo '
							<tr>
								<td><strong>'.utf8_encode($row[0]).'</strong></td>
							</tr>
							<tr>';
								echo '<td>'.utf8_encode($row[1]).' &nbsp; <u><a href='.$row[2].' target=\"_blank\" ><em>Read more...</em></a></u></td>';
								echo '</tr>
							<tr>
								<td>Distance from picture: '.$row[3].'<br><br></td>
							</tr>';
					}
				echo '</table>';
				
			}
		}	
	}
	
	# insert image categorizational related material
	echo '<br><center><h3>Image categorization related context</h3></center>';
	$arg = "id";
	
	# include pic and info context if ID is set
	if(isset($_GET[$arg])) {
		# Image id
		$id = $_GET[$arg];
		
		$conn = connectDB();
		
		// query DB
		$result = queryDB("SELECT title, summary, url, source FROM related WHERE picid=$id order by id", $conn);
		
		$num = mysql_numrows($result);
		
		closeDB($conn);
		
		if($num) {
			echo '<table width="610" border="0" cellspacing="5" cellpadding="0">';
				
				while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
					echo '
						<tr>
							<td><strong>'.utf8_encode($row[0]).'</strong></td>
						</tr>
						<tr>';
							echo '<td>'.utf8_encode($row[1]).' &nbsp; <u><a href='.$row[2].' target=\"_blank\" ><em>Read more...</em></a></u></td>';
							echo '</tr>
						<tr>
							<td>Source: '.$row[3].'<br><br></td>
						</tr>';
				}
			echo '</table>';
			
		}
	}	
	


?>


